<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="groupId" @tab-change="tabChange">
				<el-tab-pane :label="$t('sale.sale.list')" name="1"></el-tab-pane>
				<el-tab-pane :label="$t('sale.sale.shoplist')" name="2"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" v-auth="'contract.add'" @click="add" v-if="groupId == '1'"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" v-auth="'contract.delete'" :disabled="selection.length == 0" @click="batch_del" v-if="groupId == '1'"></el-button>
				<el-button type="warning" plain v-auth="'contract.check'" :disabled="selection.length == 0" @click="batch_check" v-if="groupId == '1'">{{$t('all.check')}}</el-button>
				<el-button type="info" plain v-auth="'contract.uncheck'" @click="batch_uncheck" :disabled="selection.length == 0" v-if="groupId == '1'">{{$t('all.uncheck')}}</el-button>
				<sc-export-button :auth="'contract.export'" :apiObj="apiObj" :search="search" v-if="groupId == 1"></sc-export-button>
				<sc-export-button :auth="'contract.detailsExport'" :apiObj="shopObj" :search="search" v-if="groupId == 2"></sc-export-button>
				<!-- 打印-->
				<sc-print-button :auth="'contract.print'" :selection="selection"></sc-print-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.open_single" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="To" :start-placeholder="$t('all.startdate')" :end-placeholder="$t('all.enddate')" style="width: 230px"/>
					<el-input v-model="search.contract_code" :placeholder="$t('sale.sale.code')" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.customer_name" :placeholder="$t('sale.client.client')" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.goods_name" :placeholder="$t('goods.goods.goods')" clearable @keyup.enter="upsearch" v-if="groupId == '2'"></el-input>
					<el-select v-model="search.status" filterable :placeholder="$t('all.checkstatus')" v-if="groupId == '1'" clearable>
						<el-option :label="$t('all.awit')" :value="0"/>
						<el-option :label="$t('all.checkok')" :value="1"/>
						<el-option :label="$t('all.checkno')" :value="2"/>
					</el-select>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding" v-if="groupId == '1'">
			<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" :label="$t('all.sort')" fixed align="center" width="60"></el-table-column>
				<el-table-column :label="$t('sale.sale.code')" prop="contract_code" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.client')" prop="customer.customer_name" align="center"></el-table-column>
				<el-table-column :label="$t('all.checkstatus')" prop="status" align="center">
					<template #default="scope">
						<el-tag type="warning" v-if="scope.row.status == 0">{{$t('all.awit')}}</el-tag>
						<el-tag type="success" v-if="scope.row.status == 1">{{$t('all.checkok')}}</el-tag>
						<el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.reason">
							<template #reference>
								<el-tag type="danger" v-if="scope.row.status == 2">{{$t('all.checkno')}}</el-tag>
							</template>
						</el-popover>
						<!-- <el-tag :type="scope.row.status == 0 ? 'warning' : scope.row.status == 1 ? 'success' : 'danger'">{{scope.row.status == 0 ? '待审核' : scope.row.status == 1 ? '通过' : '驳回'}}</el-tag> -->
					</template>
				</el-table-column>
				<el-table-column :label="$t('all.orderstatus')" prop="contract_status" align="center">
					<!-- <template #default="scope">
						<el-tag :type="scope.row.contract_status == 0 ? 'primary' : scope.row.contract_status == 1 ? 'success' : 'danger'">
							{{ scope.row.contract_status == 0 ? '进行中' : scope.row.contract_status == 1 ? '已完成' : '已作废' }}
						</el-tag>
					</template> -->
                    <template #default="scope">
                        <el-tag type="primary" v-if="scope.row.contract_status == 0">{{$t('all.progress')}}</el-tag><!-- 进行中 -->
                        <el-tag type="success" v-if="scope.row.contract_status == 1">{{$t('all.completed')}}</el-tag><!-- 已完成 -->
                        <el-tag type="danger" v-if="scope.row.contract_status == 2">{{$t('all.invalid')}}</el-tag><!-- 已作废 -->
                    </template>
				</el-table-column>
				<el-table-column :label="$t('all.remark')" prop="remark" align="center">
					<template #default="scope">
						<el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.remark">
							<template #reference>
								<el-button style="padding: 5px 0;font-size: 12px" text class="m-2">{{ scope.row.remark.substring(0, 10) }}</el-button>
							</template>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column :label="$t('sale.sale.date')" prop="open_single" align="center"></el-table-column>
				<el-table-column :label="$t('sale.sale.leadtime')" prop="deliver_date" align="center"></el-table-column>
				<el-table-column :label="$t('sale.sale.money')" prop="contract_price" align="center" width="100"></el-table-column>
				<el-table-column :label="$t('sale.sale.receipts')" prop="pay_price" align="center" width="100"></el-table-column>
				<!-- <el-table-column label="投产进度" prop="cate_name" align="center"></el-table-column> -->
				<!-- <el-table-column label="出库进度" prop="cate_name" align="center"></el-table-column> -->
				<!-- <el-table-column label="应收款" prop="cate_name" align="center" width="85"></el-table-column> -->
				<!-- <el-table-column label="累计支出" prop="cate_name" align="center" width="85"></el-table-column> -->
				<!-- <el-table-column label="已开票金额" prop="cate_name" align="center" width="85"></el-table-column> -->
				<el-table-column :label="$t('sale.sale.istax')" prop="is_tax" align="center">
					<template #default="scope">
                        <el-tag v-if="scope.row.is_tax == 1" type="success">{{$t('sale.sale.tax')}}</el-tag><!-- 含税 -->
                        <el-tag v-if="scope.row.is_tax == 0" type="danger">{{$t('sale.sale.untax')}}</el-tag><!-- 不含税 -->
					</template>
				</el-table-column>
				<el-table-column :label="$t('sale.sale.isticket')" prop="is_ticket" align="center">
					<template #default="scope">
                        <el-tag v-if="scope.row.is_ticket == 1" type="success">{{$t('sale.sale.ticket')}}</el-tag><!-- 开票 -->
                        <el-tag v-if="scope.row.is_ticket == 2" type="danger">{{$t('sale.sale.unticket')}}</el-tag><!-- 不开票 -->
					</template>
				</el-table-column>
				<el-table-column :label="$t('all.operate')" fixed="right" align="center" width="210">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'contract.show'">{{$t('all.show')}}</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'contract.edit'" v-if="scope.row.status != 1">{{$t('all.edit')}}</el-button>
							<el-popconfirm :title="$t('all.suredel')" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'contract.delete'" v-if="scope.row.status != 1">{{$t('all.delete')}}</el-button>
								</template>
							</el-popconfirm>
							<el-popconfirm :title="$t('all.surenullify')" @confirm="table_cancel(scope.row)">
								<template #reference>
									<el-button text type="info" size="small" v-auth="'contract.cancel'" v-if="scope.row.status == 1 && scope.row.contract_status == 0">{{$t('all.nullify')}}</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="warning" size="small" @click="table_production(scope.row, scope.$index)" v-auth="'contract.production'" v-if="scope.row.status == 1">{{$t('sale.sale.production')}}</el-button>
							<el-button text type="success" size="small" @click="table_go_plan(scope.row, scope.$index)" v-auth="'contract.show'" v-if="scope.row.status == 1">{{$t('sale.sale.purchase')}}</el-button>
							<el-button text type="warning" size="small" @click="table_copy(scope.row, scope.$index)">{{$t('all.copy')}}</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-main class="nopadding" v-if="groupId == '2'">
			<scTable ref="table" row-key="id" :apiObj="shopObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" :label="$t('all.sort')" fixed align="center" width="60"></el-table-column>
				<el-table-column :label="$t('sale.sale.code')" prop="contract_code" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.name')" prop="customer_name" align="center"></el-table-column>
				<el-table-column :label="$t('goods.goods.name')" prop="goods.goods_name" align="center"></el-table-column>
				<el-table-column :label="$t('goods.goods.code')" prop="goods.goods_code" align="center"></el-table-column>
				<el-table-column :label="$t('goods.goods.spec')" prop="goods.goods_spec" align="center"></el-table-column>
				<el-table-column :label="$t('goods.goods.unit')" prop="goods.unit.unit_name" align="center"></el-table-column>
				<el-table-column :label="$t('goods.goods.category')" prop="goods.cate.cate_name" align="center"></el-table-column>
				<el-table-column :label="$t('all.number')" prop="sale_num" align="center"></el-table-column>
				<el-table-column :label="$t('sale.sale.date')" prop="open_single" align="center"></el-table-column>
				<el-table-column :label="$t('sale.sale.leadtime')" prop="deliver_date" align="center"></el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
	<check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false"></check-status>
	<save-production v-if="dialog.production" ref="productionDialog" @success="handleSuccess" @closed="dialog.production = false"></save-production>
	<plan-save v-if="dialog.plan" ref="planDialog" @success="handleSuccess" @closed="dialog.plan = false"></plan-save>
</template>

<script>
import SaveProduction from '@/views/production/plan/save.vue'
import CheckStatus from './checkStatus.vue'
import saveDialog from './save'
import planSave from '@/views/procure/plan/save.vue'

export default {
	name: 'BOM列表',
	components: {
		saveDialog, CheckStatus, SaveProduction, planSave
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
				production: false,
				plan: false,
			},
			apiObj: this.$API.contract.contract.list,
			shopObj: this.$API.contract.contract.detailList,
			selection: [],
			search: {
				contract_code: null,
				customer_name: null,
				status: null,
				contract_status: null,
			},
			groupId: "1",
			form: {},
		}
	},
	mounted() {

	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		// 审核
		table_check(row) {
			this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('edit').setData(row)
			})
		},
		//反审核
		async table_uncheck(row) {
			var reqData = {id: [row.id], status: 0}
			var res = await this.$API.contract.contract.check.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.yes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		//作废
		async table_cancel(row) {
			var reqData = {id: [row.id], status: 0}
			var res = await this.$API.contract.contract.cancel.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.yes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		// 生产
		table_production(row) {
			this.dialog.production = true
			this.$nextTick(() => {
				this.$refs.productionDialog.open('production').goProduction(row)
			})
		},
		// 发起采购计划
		table_go_plan(row) {
			this.dialog.plan = true
			this.$nextTick(() => {
				this.$refs.planDialog.open('sale').saleCome(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.contract.contract.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.contract.contract.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`${this.$t('all.longmessage')} ${this.selection.length} ${this.$t('all.term')}`, this.$t('all.prompt'), {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		batch_check() {
			var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.status != 0) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error(this.$t('all.selectuncheck'))
				return
			}
			this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('edit').setData(ids)
			})
		},
		async batch_uncheck() {
			var ids = [];
            let go = true
            this.selection.forEach((item) => {
                if(item.status!=1){
                    go = false
                }else{
                    ids.push(item.id)
                }
            })
            if(!go){
                this.$message.error(this.$t('all.selectcheck'))
                return
            }
			this.$confirm(`${this.$t('all.confirmuncheck')} ${this.selection.length} ${this.$t('all.term')}`, this.$t('all.prompt'), {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: ids}
				var res = await this.$API.contract.contract.uncheckStatus.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success(this.$t('all.yes'))
				} else {
					this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
				}
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'production') {
				this.$refs.table.refresh()
			} else if (mode == 'sale') {
				this.$refs.table.refresh()
			} else if (mode == 'copy') {
				this.$refs.table.refresh()
			}
		},
		async table_copy(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('copy').setCopy(row)
			})
		},
	}
}
</script>

<style></style>
